package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.FirstShowAdapter;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
* 导航页
* */

public class FirstShowActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.start)
    TextView start;
    @BindView(R.id.skip_btn)
    TextView skip_btn;

    private TextView[] dots;
    private int currentIndex;// 记录当前选中位置
    private GestureDetector mygesture;
    private static final int FLING_MIN_DISTANCE = 180;
    private static final int FLING_MIN_VELOCITY = 300;
    private int[] srcs = new int[]{R.mipmap.p1, R.mipmap.p2, R.mipmap.p3};

    @OnClick(R.id.start)
    public void onViewClicked() {
        scrollToMain();
    }

    private class GuideViewTouch extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (currentIndex == (srcs.length - 1) && e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(e1.getY() - e2.getY()) < 200 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                scrollToMain();
            }
            return false;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (mygesture != null && mygesture.onTouchEvent(ev)) {
            ev.setAction(MotionEvent.ACTION_CANCEL);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        ButterKnife.bind(this);

        mygesture = new GestureDetector(this, new GuideViewTouch());

        List<ImageView> images = new ArrayList<>();
        for (int src : srcs) {
            ImageView view = new ImageView(this);
            view.setImageResource(src);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            images.add(view);
        }

        viewPager.setAdapter(new FirstShowAdapter(images));
        viewPager.addOnPageChangeListener(this);

        initDots();

        skip_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewPager.setCurrentItem(srcs.length - 1);
            }
        });
    }

    private void initDots() {
        dots = new TextView[srcs.length];
        // 循环取得小点图片
        for (int i = 0; i < srcs.length; i++) {
            dots[i] = (TextView) ll.getChildAt(i);
            dots[i].setBackgroundResource(R.mipmap.d1);// 都设为灰
            dots[i].setTextColor(getResources().getColor(R.color.text2));
        }

        currentIndex = 0;
        dots[currentIndex].setBackgroundResource(R.mipmap.d2);// 设置为白色
        dots[currentIndex].setTextColor(getResources().getColor(R.color.white));
        if (currentIndex==(srcs.length-1)){
            ll.setVisibility(View.GONE);
        }else {
            ll.setVisibility(View.VISIBLE);
        }
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > srcs.length - 1 || currentIndex == position) {
            return;
        }
        dots[position].setBackgroundResource(R.mipmap.d2);
        dots[position].setTextColor(getResources().getColor(R.color.white));
        dots[currentIndex].setBackgroundResource(R.mipmap.d1);
        dots[currentIndex].setTextColor(getResources().getColor(R.color.text2));
        currentIndex = position;
        if (currentIndex==(srcs.length-1)){
            ll.setVisibility(View.GONE);
        }else {
            ll.setVisibility(View.VISIBLE);
        }
    }

    private void scrollToMain() {
        UserInfo.setFirstStart(false);
        finish();

        startActivity(new Intent(FirstShowActivity.this, MainActivity.class));
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        start.setVisibility((i + 1) == viewPager.getAdapter().getCount() ? View.VISIBLE : View.GONE);
        skip_btn.setVisibility((i + 1) == viewPager.getAdapter().getCount() ? View.GONE : View.VISIBLE);

        setCurrentDot(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}